import { arrayToTree } from "@/utils";
export default {
  data() {
    return {
      idLabel: 'id',
      deleteTip: '此操作将永久删除该记录, 是否继续?',
      dataLoading: false,
      listData: [],
      multipleSelection: [], //多选结果
      totalSize: 0, //列表记录总数
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      provideSelectOptions: { $condition: {} },
      remoteSelectOptions: []
    }
  },
  provide() {
    return {
      selectOptionsGenerator: () => {
        return this.provideSelectOptions;
      },
    };
  },
  async created() {
    await this.getList()
    this.getSelectOptions();
  },
  methods: {
    /**
     * 详情
     */
    handleShowDetail(row) {
      // console.log(row)
      if (this.$refs.hasOwnProperty('itemDetail')) {
        this.$refs.itemDetail.show('detail', row);
      }
    },
    /**
     * 添加按钮
     */
    handleItemAdd() {
      if (this.$refs.hasOwnProperty('itemDetail')) {
        this.$refs.itemDetail.show('add');
      }
    },
    /**
     * 编辑行记录
     * @param row
     */
    handleEditItem(row) {
      if (this.$refs.hasOwnProperty('itemDetail')) {
        this.$refs.itemDetail.show('edit', row);
      }
    },

    /**
     * 删除行记录
     * @param row
     */
    handleDeleteItem(row) {
      this.$confirm(this.deleteTip, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'cancel-msgbtn', confirmButtonClass: 'confirm-msgbtn', type: 'warning',
      }).then(() => {
        this.batchDelete(row[this.idLabel]).then(res => {
          this.$message.success('删除成功');
          this.handleDel && this.handleDel()
          this.getList()
        })
      }).catch(() => { });
    },

    /**
     * 获取更新表格数据
     * @param params 筛选条件
     * @param reset 是否重置筛选条件
     */
    getList(params, reset) {
      if (this.getListProxy && !this.getListProxy()) {
        return
      }
      if (!this.filterParams) return;
      if (params) {
        this.pagination.pageSize = params.pageSize || this.pagination.pageSize
      }
      if (reset) {
        this.filterParams = params;
      } else {
        if (params) {
          Object.assign(this.filterParams, params)
        }
      }
      // 如果通过切换页码按钮，tablelist组件会emit事件上来并调用getList，这时候需要把filter里的pageNum赋值给pagination
      // 否则会导致pagination不变，而在切换查询条件时导致page显示错误
      // 问题的原因在于，切换查询条件时自己调用getList方法，没有跟table组件里的refreshData方法一样，先把pageNum复制给pagination
      if (this.filterParams.pageNum) {
        this.pagination.pageNum = this.filterParams.pageNum;
      }
      this.dataLoading = true;
      this.listData = [];
      if (this.filterParamsHandle) {
        this.filterParamsHandle();
      }
      if (this.filterParams.id) {
        return this.list(this.filterParams.id, this.filterParams).then(res => {
          if (this.rowDataHandle) {
            this.rowDataHandle(res.rows)
          } else {
            this.listData = res.rows;
          }
          this.totalSize = res.total;
          this.dataLoading = false;
          this.listCallback && this.listCallback()
        }).catch(() => {
          this.dataLoading = false
        })
      }
      return this.list(this.filterParams).then(res => {
        if (this.rowDataHandle) {
          this.rowDataHandle(res.rows)
        } else {
          this.listData = res.rows;
        }
        this.totalSize = res.total;
        this.dataLoading = false;
        this.listCallback && this.listCallback()
      }).catch(() => {
        this.dataLoading = false
      })
    },
    getSelectOptions() {
      if (!this.remoteSelectOptions.length) return
      for (const { methods, type, id, parentId, name } of this.remoteSelectOptions) {
        methods().then(({ rows }) => {
          this.$set(this.provideSelectOptions, name, rows);
          if (type === "tree") {
            this.$set(
              this.provideSelectOptions,
              name + "-tree",
              arrayToTree(rows, id, parentId)
            );
          }
        });
      }
    },
    resetFilter() {
      this.$children[0].resetFilter(1);
    }
  }
}
